package it.polimi.digitalstate.webbeans;

import it.polimi.digitalstate.dto.CitizenDTO;
import it.polimi.digitalstate.dto.UserDTO;
import it.polimi.digitalstate.manager.AccountManagerLocal;
import it.polimi.digitalstate.persist.*;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;


/**
 * 
 *
 */
@ManagedBean(name="registrationBean")
@RequestScoped
public class RegistrationBean {
	@EJB
	private AccountManagerLocal ejb;
	 
	private UserDTO user=new UserDTO();;
	private CitizenDTO citizen=new CitizenDTO();
	
	private UIComponent username;
	private UIComponent email;
	private UIComponent password;
	private UIComponent name;
	private UIComponent surname;
	private UIComponent ecode;
	private UIComponent dob;
	
	public AccountManagerLocal getEjb() {
		return ejb;
	}

	public void setEjb(AccountManagerLocal ejb) {
		this.ejb = ejb;
	}

	public UIComponent getUsername() {
		return username;
	}

	public void setUsername(UIComponent username) {
		this.username = username;
	}

	public UIComponent getEmail() {
		return email;
	}

	public void setEmail(UIComponent email) {
		this.email = email;
	}

	public UIComponent getPassword() {
		return password;
	}

	public void setPassword(UIComponent password) {
		this.password = password;
	}

	public UIComponent getName() {
		return name;
	}

	public void setName(UIComponent name) {
		this.name = name;
	}

	public UIComponent getSurname() {
		return surname;
	}

	public void setSurname(UIComponent surname) {
		this.surname = surname;
	}

	public UIComponent getEcode() {
		return ecode;
	}

	public void setEcode(UIComponent ecode) {
		this.ecode = ecode;
	}

	public UIComponent getDob() {
		return dob;
	}

	public void setDob(UIComponent dob) {
		this.dob = dob;
	}

	public RegistrationBean(){
		
	}

	public UserDTO getUser() {
		return user;
	}

	public void setUser(UserDTO user) {
		this.user = user;
	}

	public CitizenDTO getCitizen() {
		return citizen;
	}

	public void setCitizen(CitizenDTO citizen) {
		this.citizen = citizen;
	}
	
	public String register(){
		System.out.println("lavaccatafatttt");
		user.setGroupid("citizen");
		try {
			ejb.registerUser(user, citizen);
			return "registrationcorrectlydone?faces-redirect=true";
		}catch(InvalidCitizenEcode ex){
			showMessage(ecode,ex);
		}catch(UncorrectCitizenEcodeMatch ex){
			showMessage(ecode,ex);
		}catch(UniqueUserEmailException ex){
			showMessage(email, ex);
		}catch(UniqueUserUsernameException ex){
			showMessage(username, ex);
		}catch(AccountAlreadyExistingException ex){
			showMessage(username, ex);
		}catch (SuperEntityManagerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "#";
	}
	private void showMessage(UIComponent component, Exception ex){
		System.out.println("lavaccatafat");
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,ex.getLocalizedMessage(),ex.getLocalizedMessage());
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(component.getClientId(context), message);
	}
	
}
